package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.OrderItemQuery;
import cn.org.eshow.model.OrderItem;
import cn.org.eshow.model.ProductComment;
import cn.org.eshow.model.User;
import cn.org.eshow.service.OrderItemManager;
import cn.org.eshow.webapp.action.response.OrderItemResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static cn.org.eshow.webapp.util.RenderUtil.success;

/**
 * 订单明细API接口
 */
@AllowedMethods({"list", "comment"})
public class OrderItemAction extends ApiBaseAction<OrderItem> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private OrderItemManager orderItemManager;

    private OrderItem orderItem = new OrderItem();
    private ProductComment productComment=new ProductComment();
    private List<OrderItem> orderItems = new ArrayList<OrderItem>();
    private OrderItemQuery query = new OrderItemQuery();

    /**
     * 订单明细 列表
     */
    public void list() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if(query.getProductOrderId()==null){
            RenderUtil.failure("缺少订单ID");
            return;
        }
        query.setUserId(user.getId());
        orderItems = orderItemManager.list(query);
        if (orderItems.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        List<OrderItemResponse> responses = new ArrayList<OrderItemResponse>();
        for (OrderItem obj : orderItems) {
            responses.add(new OrderItemResponse(obj));
        }
        RenderUtil.list("获取成功", "orderItems", responses);
    }

    /**
     * 单个商品进行评价
     */
    public void comment(){
        OrderItem old = orderItemManager.get(id);
        if(old==null){
            RenderUtil.failure("缺少订单明细OrderItemID");
            return;
        }
        orderItemManager.comment(old,productComment);
        success("提交成功");
        return;

    }

    public OrderItem getOrderItem() {
        return orderItem;
    }

    public void setOrderItem(OrderItem orderItem) {
        this.orderItem = orderItem;
    }

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }

    public OrderItemQuery getQuery() {
        return query;
    }

    public void setQuery(OrderItemQuery query) {
        this.query = query;
    }

    public ProductComment getProductComment() {
        return productComment;
    }

    public void setProductComment(ProductComment productComment) {
        this.productComment = productComment;
    }
}